package com.zdhr.igms.report.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zdhr.igms.demain.*;
import com.zdhr.igms.report.dao.ReportDao;
import com.zdhr.igms.report.service.ReportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    private ReportDao reportDao;

    @Override
    public void insertReportInfo(ReportInfo reportInfo) throws Exception {
        int result = reportDao.insertReportInfo(reportInfo);
        if(result<1){
            throw new Exception("报表信息持久化失败!");
        }
    }

    @Override
    public void insertReportData(ReportData reportData) throws Exception {
        int result = reportDao.insertReportData(reportData);
        if(result<1){
            throw new Exception("报表列表数据信息持久化失败!");
        }
    }

    @Override
    public void insertReportStatistics(ReportStatistics reportStatistics) throws Exception {
        int result = reportDao.insertReportStatistics(reportStatistics);
        if(result<1){
            throw new Exception("报表结果持久化失败!");
        }
    }

    @Override
    public void insertReportLineData(ReportLineData reportLineData) throws Exception{
        int result = reportDao.insertReportLineData(reportLineData);
        if(result<1){
            throw new Exception("报表线性数据持久化失败!");
        }
    }

    @Override
    public void insertReport(String report) throws Exception {
        JSONArray jsonArray= JSON.parseArray(report);
        ReportInfo reportInfo;
        List<ReportData> reportDataList;
        ReportStatistics reportStatistics;
        List<ReportLineData> reportLineDataList;
        // 通过fastjson将json字符串的jsonArray对象中的各个数据取出到对应的对象中（对象或List）
        reportInfo= jsonArray.getJSONObject(0).toJavaObject(ReportInfo.class);
        reportDataList = jsonArray.getJSONArray(1).toJavaList(ReportData.class);
        reportStatistics = jsonArray.getJSONObject(2).toJavaObject(ReportStatistics.class);
        reportLineDataList = jsonArray.getJSONArray(3).toJavaList(ReportLineData.class);
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date date = simpleDateFormat.parse(reportDataList.get(0).getTime());
//        Integer id = Math.toIntExact(date.getTime());
//        System.out.println(id);
        reportDao.insertReportInfo(reportInfo);
        Integer id = reportInfo.getId();
        reportStatistics.setId(id);
//        System.out.println(reportInfo);
//        System.out.println(reportStatistics);
        reportDao.insertReportStatistics(reportStatistics);
        for (ReportData reportData: reportDataList) {
            reportData.setId(id);
            reportDao.insertReportData(reportData);
        }
        for (ReportLineData reportLineData:reportLineDataList) {
            reportLineData.setId(id);
            reportDao.insertReportLineData(reportLineData);
        }
//        System.out.println(reportDataList);
//        System.out.println(reportLineDataList);

    }

    @Override
    public void insertRealTimeData(RealTimeData realTimeData) throws Exception {
        int result = reportDao.insertRealTimeData(realTimeData);
        if(result<1){
            throw new Exception("实时数据持久化失败!");
        }
    }

    @Override
    public ReportInfo reportInfoByInfo(ReportInfo reportInfo) {
        return reportDao.reportInfoByInfo(reportInfo);
    }

    @Override
    public List<ReportData> reportDataById(ReportData reportData) {
        return reportDao.reportDataById(reportData);
    }

    @Override
    public ReportStatistics reportStatisticsById(ReportStatistics reportStatistics) {
        return reportDao.reportStatisticsById(reportStatistics);
    }

    @Override
    public List<ReportLineData> reportLineDataById(ReportLineData reportLineData) {
        return reportDao.reportLineDataById(reportLineData);
    }

    @Override
    public RealTimeData queryRealTimeDataById(String id) {
        return reportDao.queryRealTimeDataById(id);
    }

    @Override
    public List<RealTimeData> queryRealTimeDataByObject(RealTimeData realTimeData) {
        return reportDao.queryRealTimeDataByObject(realTimeData);
    }

    @Override
    public void queryShowData(PageBean page,RealTimeData realTimeData) {
        List<RealTimeData> realTimeDataList = reportDao.queryRealTimeDataByObjectPage(page,realTimeData);
        int totalRows = reportDao.queryRealTimeDataByObjectReturnCount(realTimeData);
        if(totalRows>0){
            page.setTotalRows(totalRows);
            page.setCount(totalRows);
        }else{
            page.setTotalRows(0);
            page.setCount(0);
        }
        List<Map> mapList = new ArrayList<>();
        for (RealTimeData data:realTimeDataList) {
            Map map1 = new HashMap();
            Map map2 = new HashMap();
            map1.put("dev_id",data.getDev_id());
            map1.put("channel",1);
            map1.put("hole_name",data.getHole_name1());
            map1.put("seg_name",data.getSeg_name1());
            map1.put("flow_in",data.getFlow_in1());
            map1.put("flow_out",data.getFlow_out1());
            map1.put("density",data.getDensity1());
            map1.put("shift",data.getShift1());
            map1.put("temperature",data.getTemperature1());
            map2.put("dev_id",data.getDev_id());
            map2.put("channel",2);
            map2.put("hole_name",data.getHole_name2());
            map2.put("seg_name",data.getSeg_name2());
            map2.put("flow_in",data.getFlow_in2());
            map2.put("flow_out",data.getFlow_out2());
            map2.put("density",data.getDensity2());
            map2.put("shift",data.getShift2());
            map2.put("temperature",data.getTemperature2());
            if(1 == data.getChannel()){
                map1.put("channelStatus",1);
                map2.put("channelStatus",0);
            }else if(2 == data.getChannel()){
                map1.put("channelStatus",0);
                map2.put("channelStatus",1);
            }else if(3 == data.getChannel()){
                map1.put("channelStatus",1);
                map2.put("channelStatus",1);
            }else{
                map1.put("channelStatus",0);
                map2.put("channelStatus",0);
            }
            mapList.add(map1);
            mapList.add(map2);
        }
        page.setData(mapList);
    }
}
